package ui.activity.order;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import net.ck.hzq.R;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import Bean.OrderDetailsNewBean;
import Utils.StringUtil;
import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import ui.activity.order.biz.OrderDetailsBiz;
import ui.activity.order.component.DaggerOrderDetailsComponent;
import ui.activity.order.contract.OrderDetailsContract;
import ui.activity.order.module.OrderDetailsModule;
import ui.activity.order.presenter.OrderDetailsPresenter;
import ui.activity.poscontrol.adapter.orderinfo2Adapter;
import util.StringUtils;


/**
 * 订单详情页面
 * Created by chenjian on 2017/8/4.
 */

public class OrderDetailsAct extends BaseAct implements OrderDetailsContract.View {


    @Inject
    OrderDetailsPresenter presenter;
    @Inject
    OrderDetailsBiz biz;

    @BindView(R.id.orderdetails_img)
    SimpleDraweeView orderdetailsImg;
    @BindView(R.id.orderdetails_txt3)
    TextView orderdetailsTxt3;
    @BindView(R.id.orderdetails_txt1)
    TextView orderdetailsTxt1;
    @BindView(R.id.orderdetails_txt2)
    TextView orderdetailsTxt2;
    @BindView(R.id.orderdetails_txt4)
    TextView orderdetailsTxt4;
    @BindView(R.id.orderdetails_txt5)
    TextView orderdetailsTxt5;
    @BindView(R.id.orderdetails_txt6)
    TextView orderdetailsTxt6;
    @BindView(R.id.orderdetails_txt7)
    TextView orderdetailsTxt7;
    @BindView(R.id.orderdetails_txt8)
    TextView orderdetailsTxt8;
    @BindView(R.id.activeednum)
    TextView activeednum;//激活数量
    @BindView(R.id.activeedbaifenbi)
    TextView activeedbaifenbi;//激活比例

    @BindView(R.id.order_info_recycleview)
    RecyclerView recyclerView;

    orderinfo2Adapter adapter;
    @Override
    protected void setRoot() {
//        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_orderdetails);
    }

    @Override
    protected void setup() {
        DaggerOrderDetailsComponent.builder().orderDetailsModule(new OrderDetailsModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.orderdetailes), "");


    }
    public class ScrollLinearLayoutManager extends LinearLayoutManager {

        private boolean isScrollEnable = true;

        public ScrollLinearLayoutManager(Context context) {
            super(context);
        }

        public ScrollLinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
            super(context, orientation, reverseLayout);
        }

        public ScrollLinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        public boolean canScrollVertically() {
            return isScrollEnable && super.canScrollVertically();
        }

        /**
         * 设置 RecyclerView 是否可以垂直滑动
         * @param isEnable
         */
        public void setScrollEnable(boolean isEnable) {
            this.isScrollEnable = isEnable;
        }
    }
    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        presenter.setBiz(biz);
        presenter.getOrderInfo(StringUtils.isEmpty(getIntent().getStringExtra("id")) ? "" : getIntent().getStringExtra("id"));
        ScrollLinearLayoutManager manger = new ScrollLinearLayoutManager(OrderDetailsAct.this);
        manger.setScrollEnable(false);
        recyclerView.setLayoutManager(manger);
    }

    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    public void upDateUI(OrderDetailsNewBean bean) {
        orderdetailsImg.setImageURI(Uri.parse(bean.getShopOrderPosModel().getImgPath()));
        orderdetailsTxt1.setText(bean.getShopOrderPosModel().getPosName());
        orderdetailsTxt2.setText("¥" + StringUtil.getFormatPrice(bean.getShopOrderPosModel().getPrice()));
        orderdetailsTxt3.setText("X " + bean.getShopOrderPosModel().getNumber());
        orderdetailsTxt4.setText("共" + bean.getShopOrderPosModel().getNumber() + "件商品 合计：¥ " + StringUtil.getFormatPrice(bean.getTotalAmount()));
        orderdetailsTxt5.setText(bean.getPayId());
        orderdetailsTxt6.setText(bean.getPaySerial());
        activeednum.setText(bean.getActiveCount()+"台");

        if(bean.getProportion()>=70){
            if(bean.getOrderStatus().equals("2")) {
                activeedbaifenbi.setText(bean.getProportion() + "%(已完成，代发货)");
            }else{
                activeedbaifenbi.setText(bean.getProportion() + "%");
            }
        }else {
            activeedbaifenbi.setText(bean.getProportion()+"%(未完成)");
        }

        java.text.DateFormat format1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        Date date = new Date(bean.getShopOrderPosModel().getGmtCreate());
        orderdetailsTxt7.setText(format1.format(date));
        date = new Date(bean.getPayDate());
        orderdetailsTxt8.setText(format1.format(date));

    }

    @Override
    public void upDateSnList(List<OrderDetailsNewBean.PosOrderShipVOListBean> snList) {
       if(adapter == null){
           adapter = new orderinfo2Adapter(OrderDetailsAct.this,snList);
           recyclerView.setAdapter(adapter);
       }else{
           adapter.notifyDataSetChanged();
       }
    }

}
